/*
 * PersonValueDisplay.java
 *
 * Created on 05 January 2004, 04:27
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.contactable.person;

import com.velocityme.client.gui.ValueListModel;
import com.velocityme.client.gui.contactdetail.ContactDetailTableModel;
import com.velocityme.valueobjects.ContactableValue;
import com.velocityme.valueobjects.ContactDetailValue;
import com.velocityme.valueobjects.DepartmentValue;
import com.velocityme.valueobjects.DepartmentValueToString;
import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.PersonValue;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author  Robert Crida Work
 */
public class PersonValueDisplay extends javax.swing.JPanel {
    
    private PersonValue m_personValue;
    private ValueListModel m_modelUsers;
    private ContactDetailTableModel m_modelContactDetails;
    
    /** Creates new form PersonValueDisplay */
    public PersonValueDisplay(PersonValue p_personValue, Collection p_userValues, ContactDetailValue[] p_contactDetailValues) {
        initComponents();
        setPersonValue(p_personValue, p_userValues, p_contactDetailValues);
    }
    
    public void setPersonValue(PersonValue p_personValue, Collection p_userValues, ContactDetailValue[] p_contactDetailValues) {
        if (p_personValue.firstNameHasBeenSet())
            jTextFieldFirstName.setText(p_personValue.getFirstName());
        if (p_personValue.lastNameHasBeenSet())
            jTextFieldLastName.setText(p_personValue.getLastName());
        if (p_personValue.jobTitleHasBeenSet())
            jTextFieldJobTitle.setText(p_personValue.getJobTitle());
        ContactableValue contactableValue = p_personValue.getContactableValue();
        if (contactableValue != null) {
            NodeValue nodeValue = contactableValue.getNodeValue();
            if (nodeValue != null && nodeValue.descriptionHasBeenSet())
                jTextAreaDescription.setText(nodeValue.getDescription());
        }
        jLabelUsers.setVisible(p_userValues != null);
        jScrollPaneUsers.setVisible(p_userValues != null);
        if (p_userValues != null)
            m_modelUsers.setItems(p_userValues);
        m_modelContactDetails.setContactDetails(p_contactDetailValues);
        m_personValue = p_personValue;
    }
    
    public PersonValue getPersonValue() {
        if (m_personValue.getContactableValue() == null)
            m_personValue.setContactableValue(new ContactableValue());
        if (m_personValue.getContactableValue().getNodeValue() == null)
            m_personValue.getContactableValue().setNodeValue(new NodeValue());
        m_personValue.setFirstName(jTextFieldFirstName.getText());
        m_personValue.setLastName(jTextFieldLastName.getText());
        m_personValue.setJobTitle(jTextFieldJobTitle.getText());
        m_personValue.getContactableValue().getNodeValue().setDescription(jTextAreaDescription.getText());
        return m_personValue;
    }
    
    public ContactDetailValue[] getContactDetailValues() {
        return m_modelContactDetails.getElements();
    }
    
    public void setEditable(boolean p_editable) {
        jTextFieldFirstName.setEditable(p_editable);
        jTextFieldLastName.setEditable(p_editable);
        jTextFieldJobTitle.setEditable(p_editable);
        jTextAreaDescription.setEditable(p_editable);
        m_modelContactDetails.setEditable(p_editable);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabelFirstName = new javax.swing.JLabel();
        jTextFieldFirstName = new javax.swing.JTextField();
        jLabelLastName = new javax.swing.JLabel();
        jTextFieldLastName = new javax.swing.JTextField();
        jLabelJobTitle = new javax.swing.JLabel();
        jTextFieldJobTitle = new javax.swing.JTextField();
        jDescription = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelUsers = new javax.swing.JLabel();
        jScrollPaneUsers = new javax.swing.JScrollPane();
        m_modelUsers = new ValueListModel();
        jList1 = new javax.swing.JList(m_modelUsers);
        jLabelContactDetails = new javax.swing.JLabel();
        jScrollPaneContactDetails = new javax.swing.JScrollPane();
        m_modelContactDetails = new ContactDetailTableModel();
        jTableContactDetails = new javax.swing.JTable(m_modelContactDetails);

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/person-32x32.png")));
        add(jLabel1, new java.awt.GridBagConstraints());

        jLabelFirstName.setText("First Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelFirstName, gridBagConstraints);

        jTextFieldFirstName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        add(jTextFieldFirstName, gridBagConstraints);

        jLabelLastName.setText("Last Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelLastName, gridBagConstraints);

        jTextFieldLastName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jTextFieldLastName, gridBagConstraints);

        jLabelJobTitle.setText("Job Title:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelJobTitle, gridBagConstraints);

        jTextFieldJobTitle.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jTextFieldJobTitle, gridBagConstraints);

        jDescription.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jDescription, gridBagConstraints);

        jScrollPaneDescription.setPreferredSize(new java.awt.Dimension(106, 66));
        jTextAreaDescription.setEditable(false);
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPaneDescription, gridBagConstraints);

        jLabelUsers.setText("Users:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelUsers, gridBagConstraints);

        jScrollPaneUsers.setPreferredSize(new java.awt.Dimension(3, 66));
        jScrollPaneUsers.setViewportView(jList1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPaneUsers, gridBagConstraints);

        jLabelContactDetails.setText("Contact Details:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelContactDetails, gridBagConstraints);

        jScrollPaneContactDetails.setPreferredSize(new java.awt.Dimension(106, 66));
        jScrollPaneContactDetails.setViewportView(jTableContactDetails);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPaneContactDetails, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jDescription;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelContactDetails;
    private javax.swing.JLabel jLabelFirstName;
    private javax.swing.JLabel jLabelJobTitle;
    private javax.swing.JLabel jLabelLastName;
    private javax.swing.JLabel jLabelUsers;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPaneContactDetails;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JScrollPane jScrollPaneUsers;
    private javax.swing.JTable jTableContactDetails;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldFirstName;
    private javax.swing.JTextField jTextFieldJobTitle;
    private javax.swing.JTextField jTextFieldLastName;
    // End of variables declaration//GEN-END:variables
    
}
